package com.mushuilingfeng.ddd.user.event;

import com.mushuilingfeng.ddd.user.domain.base.BusinessException;
import com.mushuilingfeng.ddd.user.domain.org.entity.OrgApply;
import com.mushuilingfeng.ddd.user.domain.org.event.OrgApplySubmitEvent;
import com.mushuilingfeng.ddd.user.domain.org.repository.IOrgApplyRepository;
import com.mushuilingfeng.ddd.user.domain.org.vo.OrgApplyResult;
import com.mushuilingfeng.ddd.framework.AsDomainEventSubscribe;
import com.mushuilingfeng.ddd.framework.event.IDomainEventListener;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author
 * @date 2024/9/24
 */
@Slf4j
@AsDomainEventSubscribe(eventCls = OrgApplySubmitEvent.class)
public class OrgApplySubmitEventSubscribe implements IDomainEventListener<OrgApplySubmitEvent> {
    @Resource
    private IOrgApplyRepository orgApplyRepository;

    @Override
    public void onEvent(OrgApplySubmitEvent event) {
        log.info("收到机构注册申请事件，OrgApplyId = {}", event.getOrgApplyId());
        OrgApply orgApply = orgApplyRepository.get(event.getOrgApplyId());
        if (null == orgApply) {
            throw new BusinessException(-1, "机构申请单不存在");
        }
        log.info("（demo示例，不实现人工审核）自动进行审核！");
        OrgApplyResult.OrgApplyResultBuilder orgApplyResultBuilder = OrgApplyResult.builder();
        orgApplyResultBuilder.checkResult("审核通过").checkTime(new Date()).checkUserAccount("system admin").checkUserName("系统管理员").checkRemark("无需人工审核");
        orgApply.doCheck(orgApplyResultBuilder.build());
    }

}
